package com.prd.tools;

import com.baomidou.mybatisplus.extension.service.IService;
import com.prd.dto.*;
import com.prd.model.*;
import com.prd.service.*;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DTOUtil {
    public static <T, E extends IService> void compareDBAndDOList(List<T> inDBList, List<T> inDOList, E service, Map<String, Object> otherParam) {
        List<T> deleteList = new ArrayList<>();
        List<T> updateList = new ArrayList<>();
        List<T> addList = new ArrayList<>();
        if (inDBList == null) {
            inDBList = new ArrayList<>();
        }
        if (inDOList == null) {
            inDOList = new ArrayList<>();
        }
        List<T> finalInDOList = inDOList;


        inDBList.forEach(inDB -> {
            if (finalInDOList.contains(inDB))
                return;
            deleteList.add(inDB);
            finalInDOList.forEach(inDO -> {
                if (inDB.hashCode() == inDO.hashCode()) {
                    updateList.add(inDO);
                    deleteList.remove(inDB);
                }
            });
        });

        addList = finalInDOList.stream()
                        .filter(t -> (!deleteList.contains(t) && !updateList.contains(t)))
                                .collect(Collectors.toList());



        deleteList.forEach(item -> {
            if (item instanceof CustomerGuestDO) {
                CustomerGuestDO guestDO = (CustomerGuestDO)item;
                ((CustomerGuestService)service).uRemoveCustomer((String) otherParam.get("customerNo"), guestDO.getGuestNo());
            }
            else if (item instanceof CustomerProjectDO) {
                CustomerProjectDO projectDO = (CustomerProjectDO)item;
                ((CustomerProjectService)service).removeByProjectId(projectDO.getProjectId());
            }
            else if (item instanceof CustomerRemarkDO) {
                CustomerRemarkDO remarkDO = (CustomerRemarkDO)item;
                ((CustomerRemarkService)service).removeById(remarkDO.getId());
            }
            else if (item instanceof CustomerContactDO) {
                CustomerContactDO contactDO = (CustomerContactDO)item;
                ((CustomerContactService)service).removeById(contactDO.getId());
            }
            else if (item instanceof CustomerCommRecordDO) {
                CustomerCommRecordDO recordDO = (CustomerCommRecordDO) item;
                ((CustomerCommRecordService)service).removeById(recordDO.getId());
            }
        });

        updateList.forEach(item -> {
            if (item instanceof CustomerGuestDO) {
                CustomerGuestDO guestDO = (CustomerGuestDO)item;
                ((CustomerGuestService)service).update(guestDO);
            }
            else if (item instanceof CustomerProjectDO) {
                CustomerProjectDO projectDO = ((CustomerProjectDO) item);
                ((CustomerProjectService)service).update(projectDO);
            }
            else if (item instanceof CustomerRemarkDO) {
                CustomerRemarkDO remarkDO = (CustomerRemarkDO) item;
                ((CustomerRemarkService)service).update(remarkDO);
            }
            else if (item instanceof CustomerContactDO) {
                CustomerContactDO contactDO = (CustomerContactDO) item;
                ((CustomerContactService)service).update(contactDO);
            }
            else if (item instanceof CustomerCommRecordDO) {
                CustomerCommRecordDO recordDO = (CustomerCommRecordDO) item;
                ((CustomerCommRecordService)service).update(recordDO);
            }
        });

        addList.forEach(item -> {
            if (item instanceof CustomerGuestDO) {
                CustomerGuestDO guestDO = (CustomerGuestDO)item;
                guestDO.setCustomerNo((String) otherParam.get("customerNo"));
                ((CustomerGuestService)service).addSimple(guestDO);
            }
            else if (item instanceof CustomerProjectDO) {
                CustomerProjectDO projectDO = ((CustomerProjectDO) item);
                projectDO.setCustomerNo((String) otherParam.get("customerNo"));
                ((CustomerProjectService)service).addSimple(projectDO);
            }
            else if (item instanceof CustomerRemarkDO) {
                CustomerRemarkDO remarkDO = (CustomerRemarkDO) item;
                remarkDO.setType((int) otherParam.get("code"));
                String no = null;
                String customerNo = (String) otherParam.get("customerNo");
                String guestNo = (String) otherParam.get("guestNo");
                String projectId = (String) otherParam.get("projectId");
                if (customerNo != null)
                    no = customerNo;
                else if (guestNo != null)
                    no = guestNo;
                else if (projectId != null)
                    no = projectId;
                remarkDO.setNo(no);
                ((CustomerRemarkService)service).addSimple(remarkDO);
            }
            else if (item instanceof CustomerContactDO) {
                CustomerContactDO contactDO = (CustomerContactDO) item;
                contactDO.setType((int) otherParam.get("code"));
                String no = (String) otherParam.get("customerNo");
                if (no == null)
                    no = (String) otherParam.get("guestNo");
                contactDO.setNo(no);
                ((CustomerContactService)service).addSimple(contactDO);
            }
            else if (item instanceof CustomerCommRecordDO) {
                CustomerCommRecordDO recordDO = (CustomerCommRecordDO) item;
                String no = (String) otherParam.get("customerNo");
                if (no == null)
                    no = (String) otherParam.get("guestNo");
                recordDO.setNo(no);
                ((CustomerCommRecordService)service).addSimple(recordDO);
            }
        });


    }


    /*---------------------- do to dto ----------------------*/
    public static CustomerDO customerDtoToDo(CustomerDTO customerDTO) {
        CustomerDO customerDO = new CustomerDO();
        CopyNonNullBeanUtils.copyProperties(customerDTO, customerDO);
        return customerDO;
    }

    public static CustomerGuestDO guestDtoToDO(CustomerGuestDTO guestDTO) {
        CustomerGuestDO guestDO = new CustomerGuestDO();
        CopyNonNullBeanUtils.copyProperties(guestDTO, guestDO);
        return guestDO;
    }

    public static CustomerProjectDO projectDtoToDO(CustomerProjectDTO projectDTO) {
        CustomerProjectDO projectDO = new CustomerProjectDO();
        CopyNonNullBeanUtils.copyProperties(projectDTO, projectDO);
        return projectDO;
    }

    public static CustomerRemarkDO remarkDtoToDO(CustomerRemarkDTO remarkDTO) {
        CustomerRemarkDO remarkDO = new CustomerRemarkDO();
        CopyNonNullBeanUtils.copyProperties(remarkDTO, remarkDO);
        return remarkDO;
    }

    public static CustomerContactDO contactDtoToDO(CustomerContactDTO contactDTO) {
        CustomerContactDO contactDO = new CustomerContactDO();
        CopyNonNullBeanUtils.copyProperties(contactDTO, contactDO);
        return contactDO;
    }

    public static CustomerCommRecordDO commRecordDtoToDO(CustomerCommRecordDTO commRecordDTO) {
        CustomerCommRecordDO commRecordDO = new CustomerCommRecordDO();
        CopyNonNullBeanUtils.copyProperties(commRecordDTO, commRecordDO);
        return commRecordDO;
    }


    /*---------------------- dto to do ----------------------*/

    public static CustomerProjectDTO projectDoToDto(CustomerProjectDO projectDO) {
        CustomerProjectDTO projectDTO = new CustomerProjectDTO();
        BeanUtils.copyProperties(projectDO, projectDTO);
        return projectDTO;
    }



}
